package com.adtech.farmProducts.activities.activity;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;

import com.adtech.farmProducts.App;
import com.adtech.farmProducts.R;
import com.adtech.farmProducts.activities.fragment.IndexFragment;
import com.adtech.farmProducts.activities.fragment.InsertListFragment;
import com.adtech.farmProducts.activities.fragment.MineFragment;
import com.adtech.farmProducts.activities.fragment.ProcurementListFragment;
import com.adtech.farmProducts.activities.fragment.ScanCodeListFragment;
import com.adtech.farmProducts.activities.fragment.VerifyListFragment;
import com.adtech.farmProducts.base.BaseNfcActivity;
import com.adtech.farmProducts.utils.AppUtil;
import com.adtech.farmProducts.utils.TabLayoutUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Time:2020/6/28
 * Created by xiaocz
 * Describe 操作记录
 */
public class OperatingDataActivity extends BaseNfcActivity {

    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    public static void show(Context context) {
        Intent intent = new Intent(context, OperatingDataActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutSourceId() {
        return R.layout.activity_operating_data;
    }

    @Override
    protected void initData() {
        super.initData();
        init();
    }

    private void init() {
        List<String> mTabTitleTexts = new ArrayList<>();
        mTabTitleTexts.add("\t 装箱\t ");
        mTabTitleTexts.add("\t 验货\t ");
        mTabTitleTexts.add("\t 上架\t ");
        mTabTitleTexts.add("\t 采购\t ");
        List<Fragment> mTabFragments = new ArrayList<>();
        mTabFragments.add(InsertListFragment.newInstance("1"));
        mTabFragments.add(VerifyListFragment.newInstance("2"));
        mTabFragments.add(ScanCodeListFragment.newInstance());
        mTabFragments.add(ProcurementListFragment.newInstance());
        mViewPager.setOffscreenPageLimit(mTabFragments.size());
        mViewPager.setAdapter(new MyViewPagerAdapter(
                getSupportFragmentManager(),
                mTabFragments,
                mTabTitleTexts));
        mTabLayout.setupWithViewPager(mViewPager);//绑定
    }

    /**
     * 定义内部的适配器类
     */
    private static class MyViewPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> mTabFragments;
        private List<String> mTabTitleTexts;

        private MyViewPagerAdapter(FragmentManager fm, List<Fragment> tabFragments, List<String> tabTitleTexts) {
            super(fm);
            mTabFragments = tabFragments;
            mTabTitleTexts = tabTitleTexts;
        }

        @Override
        public Fragment getItem(int position) {
            return mTabFragments.get(position);
        }


        @Override
        public int getCount() {
            return mTabFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTabTitleTexts.get(position);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 用户按下返回按键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
